package com.hxh.learn.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hxh.common.unification.ResultOutcome;
import com.hxh.learn.model.SysUser;
import com.hxh.learn.service.impl.SysUserServiceImpl;
import com.hxh.learn.vo.SysUserInsertVo;
import com.hxh.learn.vo.SysUserSearchVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;


/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author hxh
 * @since 2023-03-02
 */
@Controller
@RequestMapping("/learn/sysUser")
@RestController
@Api(tags = "用户管理")
public class SysUserController {

    @Autowired
    private SysUserServiceImpl sysUserService;

    @PostMapping("/add")
    @ApiOperation("添加用户")
    public ResultOutcome<SysUser> add(@Validated @RequestBody SysUserInsertVo vo) {
        boolean save = sysUserService.save(vo.convert());
        if (!save){
            return ResultOutcome.fileResult("添加失败");
        }
        return ResultOutcome.successResult();
    }

    @GetMapping("/detail")
    @ApiOperation("详情")
    public ResultOutcome<SysUser> detail(String id) {
        SysUser sysUser = sysUserService.getById(id);
        return ResultOutcome.successResult(sysUser);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户")
    public ResultOutcome<Boolean> delete(String id) {
        boolean remove = sysUserService.removeById(id);
        if (!remove){
            ResultOutcome.fileResult("删除失败");
        }
        return ResultOutcome.successResult();
    }

    @PostMapping("/list")
    @ApiOperation("分页查询")
    public ResultOutcome<Page<SysUser>> list(@RequestBody SysUserSearchVo sysUserSearchVo) {
        Page<SysUser> sysUserPage = sysUserService.queryUserList(sysUserSearchVo);
        return ResultOutcome.successResult(sysUserPage);
    }








}
